home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / markupbase.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  9KB  |  335 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Shared support for scanning document type declarations in HTML and XHTML.
  5.  
  6. This module is used as a foundation for the HTMLParser and sgmllib
  7. modules (indirectly, for htmllib as well).  It has no documented
  8. public API and should not be used directly.
  9.  
  10. '''
  11. import re
  12. _declname_match = re.compile('[a-zA-Z][-_.a-zA-Z0-9]*\\s*').match
  13. _declstringlit_match = re.compile('(\\\'[^\\\']*\\\'|"[^"]*")\\s*').match
  14. _commentclose = re.compile('--\\s*>')
  15. _markedsectionclose = re.compile(']\\s*]\\s*>')
  16. _msmarkedsectionclose = re.compile(']\\s*>')
  17. del re
  18.  
  19. class ParserBase:
  20.     '''Parser base class which provides some common support methods used
  21.     by the SGML/HTML and XHTML parsers.'''
  22.     
  23.     def __init__(self):
  24.         if self.__class__ is ParserBase:
  25.             raise RuntimeError('markupbase.ParserBase must be subclassed')
  26.  
  27.     
  28.     def error(self, message):
  29.         raise NotImplementedError('subclasses of ParserBase must override error()')
  30.  
  31.     
  32.     def reset(self):
  33.         self.lineno = 1
  34.         self.offset = 0
  35.  
  36.     
  37.     def getpos(self):
  38.         '''Return current line number and offset.'''
  39.         return (self.lineno, self.offset)
  40.  
  41.     
  42.     def updatepos(self, i, j):
  43.         if i >= j:
  44.             return j
  45.         rawdata = None.rawdata
  46.         nlines = rawdata.count('\n', i, j)
  47.         if nlines:
  48.             self.lineno = self.lineno + nlines
  49.             pos = rawdata.rindex('\n', i, j)
  50.             self.offset = j - pos + 1
  51.         else:
  52.             self.offset = self.offset + j - i
  53.         return j
  54.  
  55.     _decl_otherchars = ''
  56.     
  57.     def parse_declaration(self, i):
  58.         rawdata = self.rawdata
  59.         j = i + 2
  60.         if not rawdata[i:j] == '<!':
  61.             raise AssertionError('unexpected call to parse_declaration')
  62.         if None[j:j + 1] == '>':
  63.             return j + 1
  64.         if None[j:j + 1] in ('-', ''):
  65.             return -1
  66.         n = None(rawdata)
  67.         if rawdata[j:j + 2] == '--':
  68.             return self.parse_comment(i)
  69.         if None[j] == '[':
  70.             return self.parse_marked_section(i)
  71.         (decltype, j) = None._scan_name(j, i)
  72.         if j < 0:
  73.             return j
  74.         if None == 'doctype':
  75.             self._decl_otherchars = ''
  76.         while j < n:
  77.             c = rawdata[j]
  78.             if c == '>':
  79.                 data = rawdata[i + 2:j]
  80.                 if decltype == 'doctype':
  81.                     self.handle_decl(data)
  82.                 else:
  83.                     self.unknown_decl(data)
  84.                 return j + 1
  85.             if None in '"\'':
  86.                 m = _declstringlit_match(rawdata, j)
  87.                 if not m:
  88.                     return -1
  89.                 j = None.end()
  90.             elif c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
  91.                 (name, j) = self._scan_name(j, i)
  92.             elif c in self._decl_otherchars:
  93.                 j = j + 1
  94.             elif c == '[':
  95.                 if decltype == 'doctype':
  96.                     j = self._parse_doctype_subset(j + 1, i)
  97.                 elif decltype in ('attlist', 'linktype', 'link', 'element'):
  98.                     self.error("unsupported '[' char in %s declaration" % decltype)
  99.                 else:
  100.                     self.error("unexpected '[' char in declaration")
  101.             else:
  102.                 self.error('unexpected %r char in declaration' % rawdata[j])
  103.             if j < 0:
  104.                 return j
  105.         return -1
  106.  
  107.     
  108.     def parse_marked_section(self, i, report = 1):
  109.         rawdata = self.rawdata
  110.         if not rawdata[i:i + 3] == '<![':
  111.             raise AssertionError('unexpected call to parse_marked_section()')
  112.         (sectName, j) = None._scan_name(i + 3, i)
  113.         if j < 0:
  114.             return j
  115.         if None in ('temp', 'cdata', 'ignore', 'include', 'rcdata'):
  116.             match = _markedsectionclose.search(rawdata, i + 3)
  117.         elif sectName in ('if', 'else', 'endif'):
  118.             match = _msmarkedsectionclose.search(rawdata, i + 3)
  119.         else:
  120.             self.error('unknown status keyword %r in marked section' % rawdata[i + 3:j])
  121.         if not match:
  122.             return -1
  123.         if None:
  124.             j = match.start(0)
  125.             self.unknown_decl(rawdata[i + 3:j])
  126.         return match.end(0)
  127.  
  128.     
  129.     def parse_comment(self, i, report = 1):
  130.         rawdata = self.rawdata
  131.         if rawdata[i:i + 4] != '<!--':
  132.             self.error('unexpected call to parse_comment()')
  133.         match = _commentclose.search(rawdata, i + 4)
  134.         if not match:
  135.             return -1
  136.         if None:
  137.             j = match.start(0)
  138.             self.handle_comment(rawdata[i + 4:j])
  139.         return match.end(0)
  140.  
  141.     
  142.     def _parse_doctype_subset(self, i, declstartpos):
  143.         rawdata = self.rawdata
  144.         n = len(rawdata)
  145.         j = i
  146.         while j < n:
  147.             c = rawdata[j]
  148.             if c == '<':
  149.                 s = rawdata[j:j + 2]
  150.                 if s == '<':
  151.                     return -1
  152.                 if None != '<!':
  153.                     self.updatepos(declstartpos, j + 1)
  154.                     self.error('unexpected char in internal subset (in %r)' % s)
  155.                 if j + 2 == n:
  156.                     return -1
  157.                 if None + 4 > n:
  158.                     return -1
  159.                 if None[j:j + 4] == '<!--':
  160.                     j = self.parse_comment(j, report = 0)
  161.                     if j < 0:
  162.                         return j
  163.                 (name, j) = self._scan_name(j + 2, declstartpos)
  164.                 if j == -1:
  165.                     return -1
  166.                 if None not in ('attlist', 'element', 'entity', 'notation'):
  167.                     self.updatepos(declstartpos, j + 2)
  168.                     self.error('unknown declaration %r in internal subset' % name)
  169.                 meth = getattr(self, '_parse_doctype_' + name)
  170.                 j = meth(j, declstartpos)
  171.                 if j < 0:
  172.                     return j
  173.             if c == '%':
  174.                 if j + 1 == n:
  175.                     return -1
  176.                 (s, j) = None._scan_name(j + 1, declstartpos)
  177.                 if j < 0:
  178.                     return j
  179.                 if None[j] == ';':
  180.                     j = j + 1
  181.                 
  182.             if c == ']':
  183.                 j = j + 1
  184.                 while j < n and rawdata[j].isspace():
  185.                     j = j + 1
  186.                 if j < n:
  187.                     if rawdata[j] == '>':
  188.                         return j
  189.                     None.updatepos(declstartpos, j)
  190.                     self.error('unexpected char after internal subset')
  191.                 else:
  192.                     return -1
  193.             if c.isspace():
  194.                 j = j + 1
  195.                 continue
  196.             self.updatepos(declstartpos, j)
  197.             self.error('unexpected char %r in internal subset' % c)
  198.         return -1
  199.  
  200.     
  201.     def _parse_doctype_element(self, i, declstartpos):
  202.         (name, j) = self._scan_name(i, declstartpos)
  203.         if j == -1:
  204.             return -1
  205.         rawdata = None.rawdata
  206.         if '>' in rawdata[j:]:
  207.             return rawdata.find('>', j) + 1
  208.  
  209.     
  210.     def _parse_doctype_attlist(self, i, declstartpos):
  211.         rawdata = self.rawdata
  212.         (name, j) = self._scan_name(i, declstartpos)
  213.         c = rawdata[j:j + 1]
  214.         if c == '':
  215.             return -1
  216.         if None == '>':
  217.             return j + 1
  218.         (name, j) = self._scan_name(j, declstartpos)
  219.         if j < 0:
  220.             return j
  221.         c = None[j:j + 1]
  222.         if c == '':
  223.             return -1
  224.         if None == '(':
  225.             if ')' in rawdata[j:]:
  226.                 j = rawdata.find(')', j) + 1
  227.             else:
  228.                 return -1
  229.             if rawdata[j:j + 1].isspace():
  230.                 j = j + 1
  231.             if not rawdata[j:]:
  232.                 return -1
  233.         (name, j) = self._scan_name(j, declstartpos)
  234.         c = rawdata[j:j + 1]
  235.         if not c:
  236.             return -1
  237.         if None in '\'"':
  238.             m = _declstringlit_match(rawdata, j)
  239.             if m:
  240.                 j = m.end()
  241.             else:
  242.                 return -1
  243.             c = None[j:j + 1]
  244.             if not c:
  245.                 return -1
  246.         if c == '#':
  247.             if rawdata[j:] == '#':
  248.                 return -1
  249.             (name, j) = None._scan_name(j + 1, declstartpos)
  250.             if j < 0:
  251.                 return j
  252.             c = None[j:j + 1]
  253.             if not c:
  254.                 return -1
  255.         if c == '>':
  256.             return j + 1
  257.  
  258.     
  259.     def _parse_doctype_notation(self, i, declstartpos):
  260.         (name, j) = self._scan_name(i, declstartpos)
  261.         if j < 0:
  262.             return j
  263.         rawdata = None.rawdata
  264.         while None:
  265.             c = rawdata[j:j + 1]
  266.             if not c:
  267.                 return -1
  268.             if None == '>':
  269.                 return j + 1
  270.             if None in '\'"':
  271.                 m = _declstringlit_match(rawdata, j)
  272.                 if not m:
  273.                     return -1
  274.                 j = None.end()
  275.                 continue
  276.             (name, j) = self._scan_name(j, declstartpos)
  277.             if j < 0:
  278.                 return j
  279.             return None
  280.  
  281.     
  282.     def _parse_doctype_entity(self, i, declstartpos):
  283.         rawdata = self.rawdata
  284.         if rawdata[i:i + 1] == '%':
  285.             j = i + 1
  286.             while None:
  287.                 c = rawdata[j:j + 1]
  288.                 if not c:
  289.                     return -1
  290.                 if None.isspace():
  291.                     j = j + 1
  292.                     continue
  293.                 break
  294.                 continue
  295.         j = i
  296.         (name, j) = self._scan_name(j, declstartpos)
  297.         if j < 0:
  298.             return j
  299.         c = self.rawdata[j:j + 1]
  300.         if not c:
  301.             return -1
  302.         if None in '\'"':
  303.             m = _declstringlit_match(rawdata, j)
  304.             if m:
  305.                 j = m.end()
  306.             else:
  307.                 return -1
  308.         if c == '>':
  309.             return j + 1
  310.         (name, j) = None._scan_name(j, declstartpos)
  311.         if j < 0:
  312.             return j
  313.  
  314.     
  315.     def _scan_name(self, i, declstartpos):
  316.         rawdata = self.rawdata
  317.         n = len(rawdata)
  318.         if i == n:
  319.             return (None, -1)
  320.         m = None(rawdata, i)
  321.         if m:
  322.             s = m.group()
  323.             name = s.strip()
  324.             if i + len(s) == n:
  325.                 return (None, -1)
  326.             return (None.lower(), m.end())
  327.         None.updatepos(declstartpos, i)
  328.         self.error('expected name token at %r' % rawdata[declstartpos:declstartpos + 20])
  329.  
  330.     
  331.     def unknown_decl(self, data):
  332.         pass
  333.  
  334.  
  335.